﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace SmartMeterParamFrame
{
    /// <summary>
    /// 加密机访问接口
    /// </summary>
    internal static class API
    {
        /// <summary>
        /// 打开加密狗
        /// </summary>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int OpenUsbkey();


        /// <summary>
        /// 服务器登陆
        /// </summary>
        /// <param name="IP">IP</param>
        /// <param name="Port">端口</param>
        /// <param name="nPwdLen">密码长度</param>
        /// <param name="Pwd">密码</param>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int LgServer(StringBuilder IP, ushort Port, int nPwdLen, StringBuilder Pwd);

        /// <summary>
        /// 服务器登录
        /// </summary>
        /// <param name="IP">IP</param>
        /// <param name="Port">端口</param>
        /// <param name="nPwdLen">密码长度</param>
        /// <param name="Pwd">密码</param>
        /// <param name="iPtr">电科院认证字串</param>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int LgServer(StringBuilder IP, ushort Port, int nPwdLen, StringBuilder Pwd, IntPtr iPtr);

        /// <summary>
        /// 断开服务器连接
        /// </summary>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int LgoutServer();

        /// <summary>
        /// 释放服务器登录权限
        /// </summary>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int ClseUsbkey();

        /// <summary>
        /// 远程身份认证函数
        /// </summary>
        /// <param name="Flag">当前密钥状态（0-公钥状态下身份认证，1-私钥状态身份认证）</param>
        /// <param name="PutDiv">表示输入的分散因子，字符型，长度16，“0000”+表通信地址</param>
        /// <param name="OutRand">输出的随机数，长度16</param>
        /// <param name="OutEndata">输出的密文，长度16</param>
        /// <param name="NameId">调用者ID</param>
        /// <returns>0-成功，其他失败</returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int IdentityAuthentication(int Flag, StringBuilder PutDiv, StringBuilder OutRand, StringBuilder OutEndata, StringBuilder NameId);

        /// <summary>
        /// 远程控制函数
        /// </summary>
        /// <param name="Flag">表示当前密钥状态，0-公钥状态</param>
        /// <param name="PutRand">输入随机数，长度8</param>
        /// <param name="PutDiv">输入分散因子，长度16</param>
        /// <param name="PutEsamNo">输入ESAM序列号，长度16</param>
        /// <param name="PutData">输入控制命令明</param>
        /// <param name="OutEndata">输出密文</param>
        /// <returns>0-成功，其他失败</returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int UserControl(int Flag, StringBuilder PutRand, StringBuilder PutDiv, StringBuilder PutEsamNo, StringBuilder PutData, StringBuilder OutEndata);

        /// <summary>
        /// 一类参数MAC计算函数（02）
        /// </summary>
        /// <param name="Flag">恒为0</param>
        /// <param name="PutRand">表示输入的随机数,字符型,长度16 “0000”+表号</param>
        /// <param name="PutDiv">表示输入的分散因子,字符型,长度16</param>
        /// <param name="PutApdu">一类参数设置的写Esam 命令头，字符型，长度10</param>
        /// <param name="PutData">表示输入的一类参数明文,字符型</param>
        /// <param name="OutEndata">输出的MAC 数据，字符型，长度8</param>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int ParameterUpdate(int Flag, StringBuilder PutRand, StringBuilder PutDiv, StringBuilder PutApdu, StringBuilder PutData, StringBuilder OutEndata);

        /// <summary>
        /// 一类参数MAC计算函数（03）
        /// </summary>
        /// <param name="Flag">恒为0</param>
        /// <param name="PutRand">表示输入的随机数,字符型,长度16 “0000”+表号</param>
        /// <param name="PutDiv">表示输入的分散因子,字符型,长度16</param>
        /// <param name="PutApdu">一类参数设置的写Esam 命令头，字符型，长度10</param>
        /// <param name="PutData">表示输入的一类参数明文,字符型</param>
        /// <param name="OutEndata">输出的MAC 数据，字符型，长度8</param>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int ParameterUpdate1(int Flag, StringBuilder PutRand, StringBuilder PutDiv, StringBuilder PutApdu, StringBuilder PutData, StringBuilder OutEndata);

        /// <summary>
        /// 一类参数MAC计算函数（04）
        /// </summary>
        /// <param name="Flag">恒为0</param>
        /// <param name="PutRand">表示输入的随机数,字符型,长度16 “0000”+表号</param>
        /// <param name="PutDiv">表示输入的分散因子,字符型,长度16</param>
        /// <param name="PutApdu">一类参数设置的写Esam 命令头，字符型，长度10</param>
        /// <param name="PutData">表示输入的一类参数明文,字符型</param>
        /// <param name="OutEndata">输出的MAC 数据，字符型，长度8</param>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int ParameterUpdate2(int Flag, StringBuilder PutRand, StringBuilder PutDiv, StringBuilder PutApdu, StringBuilder PutData, StringBuilder OutEndata);

        /// <summary>
        /// 远程二类参数设置函数
        /// </summary>
        /// <param name="Flag">当前密钥状态，0-公钥</param>
        /// <param name="PutRand">输入随机数，长度8</param>
        /// <param name="PutDiv">输入分散因子，长度16</param>
        /// <param name="PutApdu">输入的指令数据，字符型，长度10</param>
        /// <param name="PutData">输入的二类明文，字符型</param>
        /// <param name="OutEndata">输出的密文</param>
        /// <returns>0-成功，其他失败</returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int ParameterElseUpdate(int Flag, StringBuilder PutRand, StringBuilder PutDiv, StringBuilder PutApdu, StringBuilder PutData, StringBuilder OutEndata);

        /// <summary>
        /// 远程密钥信息清零函数
        /// </summary>
        /// <param name="Flag">当前密钥状态，0-公钥，1-私钥</param>
        /// <param name="rand">输入的随机数，长度8</param>
        /// <param name="div">输入的分散因子，长度16，“0000”+表号</param>
        /// <param name="EsamNo">ESAM序列号，长度16</param>
        /// <param name="PutKeyinfo1">输入的主控密钥，密钥信息明文</param>
        /// <param name="Outkey1">输出的主控密钥密文，长度64</param>
        /// <param name="Outinfo1">输出的主控密钥信息，长度8</param>
        /// <returns>0-成功，其他失败</returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int ClearKeyInfo(int Flag, StringBuilder rand, StringBuilder div, StringBuilder EsamNo, StringBuilder PutKeyinfo1, byte[] Outkey1, StringBuilder Outinfo1);

        /// <summary>
        /// ．获取远程密钥函数
        /// </summary>
        /// <param name="Counter">恒为0</param>
        /// <param name="PutRand">表示输入的随机数,字符型,长度16</param>
        /// <param name="PutDiv">表示输入的分散因子,字符型,长度16</param>
        /// <param name="PutEsamNo">表示输入的ESAM 序列号,16</param>
        /// <param name="PutKeyinfo1">表示输入的主控密钥密钥信息明文,字符型</param>
        /// <param name="PutKeyinfo2">表示输入的远程控制密钥信息明文,字符型</param>
        /// <param name="PutKeyinfo3">表示输入的二类参数设置密钥信息明文,字符型</param>
        /// <param name="PutKeyinfo4">表示输入的远程身份认证密钥信息明文,字符型</param>
        /// <param name="OutKey1">输出的主控密钥密文,字符型，长度64</param>
        /// <param name="OutKeyinfo1">输出的主控密钥信息,字符型，长度8</param>
        /// <param name="OutKey2">输出的远程控制密钥密文,字符型，长度64</param>
        /// <param name="OutKeyinfo2">输出的远程控制密钥信息,字符型，长度8</param>
        /// <param name="OutKey3">输出的二类参数设置密钥密文,字符型，长度64</param>
        /// <param name="OutKeyinfo3">输出的二类参数设置密钥信息,字符型，长度8</param>
        /// <param name="OutKey4">输出的远程身份认证密钥密文,字符型，长度64</param>
        /// <param name="OutKeyinfo4">输出的远程身份认证密钥信息,字符型，长度8</param>
        /// <returns></returns>
        [DllImport("SJJ1009Server.dll")]
        public static extern int KeyUpdate(int Counter, StringBuilder PutRand, StringBuilder PutDiv, StringBuilder PutEsamNo,
                                            StringBuilder PutKeyinfo1, StringBuilder PutKeyinfo2, StringBuilder PutKeyinfo3, StringBuilder PutKeyinfo4,
                                            StringBuilder OutKey1, StringBuilder OutKeyinfo1, StringBuilder OutKey2, StringBuilder OutKeyinfo2,
                                            StringBuilder OutKey3, StringBuilder OutKeyinfo3, StringBuilder OutKey4, StringBuilder OutKeyinfo4);
    }
}
